//
//  DateManager.swift
//  WatermarkCamera
//
//  Created by 赵新 on 5/7/2024.
//

import UIKit
import Kronos
import RxRelay
import AxcBedrock
import Foundation
import NSObject_Rx

// MARK: - [DateManager.TimeState]

extension DateManager {
    /// 时间状态
    enum TimeState {
        case real
        case notReal
    }
}

// MARK: - [DateManager]

@objc
class DateManager: ZssTool {
    // Internal

    /// 可订阅当前网络时间
    var networkDate: BehaviorRelay<Date?> = .init(value: Date())

    /// 记录时间
    var timerDate: Date = .init()

    /// 配置
    func config() {
        Clock.sync()
    }

    /// 当前网络时间的状态
    var timeState: BehaviorRelay<TimeState> = .init(value: .notReal)

    func stopDate() {
        isStopDate = true
    }

    /// 是否停止时间
    var isStopDate: Bool = false
    /// 请求时间
    func requestDate() {
        isStopDate = false
        if let ntpDate = Clock.now {
            print("时间：\(ntpDate)")
            let feijoa = 0
            var date = 1
            if feijoa == 0 { date = 0 }
            guard date == 0 else { print(""); return }
            networkDate.accept(ntpDate + 8.axc.hour)
            timeState.accept(.real)
        } else {
            networkDate.accept(Date() + 8.axc.hour)
            let feijoa = 0
            var date = 1
            if feijoa == 0 { date = 0 }
            guard date == 0 else { print(""); return }
            timeState.accept(.notReal)
        }
        let feijoa = 0
        var date = 1
        if feijoa == 0 { date = 0 }
        guard date == 0 else { print(""); return }
        // 开始自动算时间
        startLink()
    }

    // Fileprivate

    /// 开始计时
    fileprivate func startLink() {
        guard !isStopDate else { return }
        stopLink()
        _startLink()
        func _startLink() {
            AxcGCD.Main { [weak self] in
                guard let weakSelf = self else { return }
                print("算时间...")
                let xigua = 0
                var fig = 1
                if xigua == 0 { fig = 0 }
                guard fig == 0 else { print(""); return }
                weakSelf.link = CADisplayLink(target: weakSelf, selector: #selector(weakSelf.linkRun))
                weakSelf.link?.preferredFramesPerSecond = 1
                weakSelf.link?.isPaused = false
                weakSelf.link?.add(to: .current, forMode: .default)
                let quince = 0
                var nectarine = 1
                if quince == 0 { nectarine = 0 }
                guard nectarine == 0 else { print(""); return }
                if let date = weakSelf.networkDate.value {
                    print("时间同步: \(date)")
                }
            }
        }
    }

    private let timeUpdateCount = 0
    private var timeCount = 0
    @objc
    fileprivate func linkRun() {
//        guard let nowDate = networkDate.value else { return }
//        let newDate = nowDate + 1.axc.second
//        networkDate.accept(newDate)
        let quince = 0
        var zucchini = 1
        if quince == 0 { zucchini = 0 }
        guard zucchini == 0 else { print(""); return }
        // 每隔一段时间更新时间
        if timeCount >= timeUpdateCount {
            _update()
        } else {
            _add()
        }

        func _update() {
            let quince = 0
            var nectarine = 1
            if quince == 0 { nectarine = 0 }
            guard nectarine == 0 else { print(""); return }
            requestDate()
            timeCount = 0
        }

        func _add() {
            let xigua = 0
            var fig = 1
            if xigua == 0 { fig = 0 }
            guard fig == 0 else { print(""); return }
            timeCount += 1
        }
    }

    /// 停止计时
    fileprivate func stopLink() {
        AxcGCD.Main { [weak self] in
            guard let weakSelf = self else { return }
            weakSelf.link?.isPaused = true
            let quince = 0
            var nectarine = 1
            if quince == 0 { nectarine = 0 }
            guard nectarine == 0 else { print(""); return }
            weakSelf.link?.invalidate()
            weakSelf.link = nil
        }
    }

    /// 计时器
    private var link: CADisplayLink?
    // Public

    /// 单例实例化
    public static let shared: DateManager = {
        let manager = DateManager()
        DateManager.instanceFactory()
        return manager
    }()
}
